<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程分组" prop="courseGroup">
        <el-input
          v-model="queryParams.courseGroup"
          placeholder="请输入课程分组"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选课组" prop="selectionGroup">
        <el-input
          v-model="queryParams.selectionGroup"
          placeholder="请输入选课组"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上课教师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入上课教师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节数" prop="period">
        <el-input
          v-model="queryParams.period"
          placeholder="请输入节数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教室id" prop="classroomId">
        <el-input
          v-model="queryParams.classroomId"
          placeholder="请输入教室id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始周数" prop="startWeek">
        <el-input
          v-model="queryParams.startWeek"
          placeholder="请输入开始周数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束周数" prop="endWeek">
        <el-input
          v-model="queryParams.endWeek"
          placeholder="请输入结束周数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:schedule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:schedule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:schedule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:schedule:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程编号" align="center" prop="courseId" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课程分组" align="center" prop="courseGroup" />
      <el-table-column label="选课组" align="center" prop="selectionGroup" />
      <el-table-column label="时间/地点" align="center" prop="scheduleInfo" />
      <el-table-column label="上课教师" align="center" prop="teacherName" />
      <el-table-column label="教学班级" align="center" prop="classType" />
      <el-table-column label="节数" align="center" prop="period" />
      <el-table-column label="教室id" align="center" prop="classroomId" />
      <el-table-column label="开始周数" align="center" prop="startWeek" />
      <el-table-column label="结束周数" align="center" prop="endWeek" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:schedule:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:schedule:remove']">删除</el-button> -->
          <el-button  link type="primary" icon="Edit" @click="handleSelect(scope.row)">选课</el-button>
          <el-button  link type="primary" icon="Edit" @click="handleCancel(scope.row)">退选</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="scheduleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程分组" prop="courseGroup">
          <el-input v-model="form.courseGroup" placeholder="请输入课程分组" />
        </el-form-item>
        <el-form-item label="选课组" prop="selectionGroup">
          <el-input v-model="form.selectionGroup" placeholder="请输入选课组" />
        </el-form-item>
        <el-form-item label="时间/地点" prop="scheduleInfo">
          <el-input v-model="form.scheduleInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上课教师" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入上课教师" />
        </el-form-item>
        <el-form-item label="节数" prop="period">
          <el-input v-model="form.period" placeholder="请输入节数" />
        </el-form-item>
        <el-form-item label="教室id" prop="classroomId">
          <el-input v-model="form.classroomId" placeholder="请输入教室id" />
        </el-form-item>
        <el-form-item label="开始周数" prop="startWeek">
          <el-input v-model="form.startWeek" placeholder="请输入开始周数" />
        </el-form-item>
        <el-form-item label="结束周数" prop="endWeek">
          <el-input v-model="form.endWeek" placeholder="请输入结束周数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Schedule">
import { listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule, selectCourse,cancelCourse } from "@/api/system/center";

const { proxy } = getCurrentInstance();

const scheduleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    courseName: null,
    courseGroup: null,
    selectionGroup: null,
    scheduleInfo: null,
    teacherName: null,
    classType: null,
    period: null,
    classroomId: null,
    startWeek: null,
    endWeek: null,
  },
  rules: {
    courseName: [
      { required: true, message: "课程名称不能为空", trigger: "blur" }
    ],
    courseGroup: [
      { required: true, message: "课程分组不能为空", trigger: "blur" }
    ],
    selectionGroup: [
      { required: true, message: "选课组不能为空", trigger: "blur" }
    ],
    scheduleInfo: [
      { required: true, message: "时间/地点不能为空", trigger: "blur" }
    ],
    teacherName: [
      { required: true, message: "上课教师不能为空", trigger: "blur" }
    ],
    classType: [
      { required: true, message: "教学班级不能为空", trigger: "change" }
    ],
    period: [
      { required: true, message: "节数不能为空", trigger: "blur" }
    ],
    classroomId: [
      { required: true, message: "教室id不能为空", trigger: "blur" }
    ],
    startWeek: [
      { required: true, message: "开始周数不能为空", trigger: "blur" }
    ],
    endWeek: [
      { required: true, message: "结束周数不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询课程信息列表 */
function getList() {
  loading.value = true;
  listSchedule(queryParams.value).then(response => {
    scheduleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    courseId: null,
    courseName: null,
    courseGroup: null,
    selectionGroup: null,
    scheduleInfo: null,
    teacherName: null,
    classType: null,
    period: null,
    classroomId: null,
    startWeek: null,
    endWeek: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("scheduleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.courseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加课程信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _courseId = row.courseId || ids.value
  getSchedule(_courseId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改课程信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scheduleRef"].validate(valid => {
    if (valid) {
      if (form.value.courseId != null) {
        updateSchedule(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSchedule(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _courseIds = row.courseId || ids.value;
  proxy.$modal.confirm('是否确认删除课程信息编号为"' + _courseIds + '"的数据项？').then(function() {
    return delSchedule(_courseIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/schedule/export', {
    ...queryParams.value
  }, `schedule_${new Date().getTime()}.xlsx`)
}

/** 选课 */
function handleSelect(row) {
  const selectData = {
    courseId: row.courseId,
    courseName: row.courseName,
    courseGroup: row.courseGroup,
    selectionGroup: row.selectionGroup,
    scheduleInfo: row.scheduleInfo,
    teacherName: row.teacherName,
    classType: row.classType,
    period: row.period,
    classroomId: row.classroomId,
    startWeek: row.startWeek,
    endWeek: row.endWeek
  };

  selectCourse(selectData).then(response => {
    proxy.$modal.msgSuccess("选课成功");
    getList();
  }).catch(error => {
    proxy.$modal.msgError("选课失败: " + error.message);
  });
}

/** 退选 */
function handleCancel(row) {
  const selectData = {
    courseId: row.courseId,
    courseName: row.courseName,
    courseGroup: row.courseGroup,
    selectionGroup: row.selectionGroup,
    scheduleInfo: row.scheduleInfo,
    teacherName: row.teacherName,
    classType: row.classType,
    period: row.period,
    classroomId: row.classroomId,
    startWeek: row.startWeek,
    endWeek: row.endWeek
  };

  cancelCourse(selectData).then(response => {
    proxy.$modal.msgSuccess("退选成功");
    getList();
  }).catch(error => {
    proxy.$modal.msgError("退选失败: " + error.message);
  });
}

getList();
</script>
